package org.sharing.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.sharing.pojo.Official;
import org.sharing.pojo.Sharing;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class SharingDaoImpl extends HibernateDaoSupport implements SharingDao{

	OfficialDao officialDao;
	
	public void setOfficialDao(OfficialDao officialDao) {
		this.officialDao = officialDao;
	}

	public void save(Sharing sharing) {
		getHibernateTemplate().save(sharing);
	}

	public void update(Sharing sharing) {
		getHibernateTemplate().update(sharing);
	}
	
	public void del(Sharing sharing) {
		getHibernateTemplate().delete(sharing);	
	}
	
	public Sharing find(final int sharingId,final int userId) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Sharing.class);
				cri.add(Restrictions.eq("userId", userId));
				cri.add(Restrictions.eq("sharingId", sharingId));
				cri.add(Restrictions.eq("type", new Integer(0)));
				List<Sharing> result = cri.list();
				return result;
			}
		});
		System.out.println(list.size());
		System.out.println("aaaaaaaa");
		return (Sharing)list.get(0);
	}
	
	public List findByPage(final int offset,final int userId) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Sharing.class);
				cri.add(Restrictions.eq("userId", userId));
				cri.add(Restrictions.eq("type", new Integer(0)));
				List<Sharing> result = cri.setFirstResult(offset*23).setMaxResults(23).list();
				List<Official> resultList = new ArrayList<Official>();
				for(int i = 0 ; i < result.size(); i++)
				{
					Official official = officialDao.get(result.get(i).getSharingId());
					resultList.add(official);
				}
				return resultList;
			}
		
		});
		return list;
	}
	
	public List findByPage(final int offset) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Sharing.class);
				cri.add(Restrictions.eq("type", new Integer(0)));
				List<Sharing> result = cri.setFirstResult(offset*24).setMaxResults(24).list();
				return result;
			}
		
		});
		return list;
	}
	
	public List<Integer> getCount() {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Sharing.class);
				cri.add(Restrictions.eq("type", new Integer(0)));
				int total = cri.list().size();
				int page;
				if(total%24==0)
					page = total/24;
				else
					page = total/24+1;
				List<Integer> result = new ArrayList<Integer>();
				for(int i = 1; i <= page; i++)
					result.add(i);
				return result;
			}
		});
		return list;
	}
}
